package binary_search.leetcode_33_medium;

public class BestSolution {
    /**
     * 中间元素与右边界元素比较，使用右中位数
     */
    public int solution1(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        while (left < right) {
            int mid = left + (right - left + 1) / 2;
            //[mid...right] 有序
            if (nums[mid] < nums[right]) {
                if (nums[mid] <= target && target <= nums[right]) {
                    left = mid;
                } else {
                    right = mid - 1;
                }
            } else {
                //[left...mid]有序
                //为了与上面保持同样的收缩行为，所以使用nums[mid - 1]
                if (nums[left] <= target && target <= nums[mid - 1]) {
                    right = mid - 1;
                } else {
                    left = mid;
                }
            }
        }
        //可能不存在target，进行判断
        return nums[left] == target ? left : -1;
    }

    /**
     * 中间元素与右边界元素比较，使用左中位数
     */
    public int solution2(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            //[mid...right] 有序
            if (nums[mid] < nums[right]) {
                //使用左中位数，需满足left = mid + 1，因此使用nums[mid + 1]
                if (nums[mid + 1] <= target && target <= nums[right]) {
                    left = mid + 1;
                } else {
                    right = mid;
                }
            } else {
                //[left...mid] 有序
                //与上面保持同样的收缩行为
                if (nums[left] <= target && target <= nums[mid]) {
                    right = mid;
                } else {
                    left = mid + 1;
                }
            }
        }
        //可能不存在target，进行判断
        return nums[left] == target ? left : -1;
    }
    /**
     * 中间元素与左边界元素比较，使用右中位数
     */
    public int solution3(int[] nums, int target){
        int left = 0;
        int right = nums.length - 1;
        while(left < right){
            int mid = left + (right - left + 1) / 2;
            //[left...mid] 有序
            if(nums[mid] > nums[left]){
                //使用右中位数，需满足left = mid，因此这里要使用nums[mid - 1]
                if(nums[left] <= target && target <= nums[mid - 1]){
                    right = mid - 1;
                }else{
                    left = mid;
                }
            }else{
                //[mid...right] 有序
                //与上面保持同样的收缩行为
                if(nums[mid] <= target && target <= nums[right]){
                    left = mid;
                }else{
                    right = mid - 1;
                }
            }
        }
        //可能不存在target，进行判断
        return nums[left] == target ? left : -1;
    }
    /**
     * 中间元素与左边界元素比较，使用左中位数
     */
    public int solution4(int[] nums, int target){
        int left = 0;
        int right = nums.length - 1;
        while(left < right){
            int mid = left + (right - left) / 2;
            //[left...mid] 有序
            if(nums[mid] > nums[left]){
                if(nums[left] <= target && target <= nums[mid]){
                    right = mid;
                }else{
                    left = mid + 1;
                }
            }else{
                //[mid..right] 有序
                //为了与上面保持一致的收缩行为，所以使用num[mid + 1]
                if(nums[mid + 1] <= target && target <= nums[right]){
                    left = mid + 1;
                }else{
                    right = mid;
                }
            }
        }
        //可能不存在target，进行判断
        return nums[left] == target ? left : -1;
    }
    /**
     * 循环内部判断 nums[mid] == target
     */
    public int solution5(int[] nums, int target){
        int left = 0;
        int right = nums.length - 1;
        while(left < right){
            int mid = left + (right - left) / 2;
            if(nums[mid] == target){
                return mid;
            }
            if(nums[mid] > nums[right]){
                if(nums[left] <= target && target < nums[mid]){
                    right = mid - 1;
                }else{
                    left = mid + 1;
                }
            }else{
                if(nums[mid] < target && target <= nums[right]){
                    left = mid + 1;
                }else{
                    right = mid - 1;
                }
            }
        }
        return nums[left] == target ? left : -1;
    }
}
